<h1 class="right">إدارة التأجير و الحجوزات</h1>

<a href="#add_client" class="create-booking-pop-up">
  <img src="images/add-icon.png" align="left" class="left" alt=""/>
</a>
<div id="add_client" class="zoom-anim-dialog mfp-hide add-booking-popup">
<?php require_once("Bookings/Create.php"); ?>
</div>

<span class="left grey medium margin-left11 margin-top5"><?php echo date('g:ia d/m/Y')?></span>
<div class="clear"></div>

<div id="dashboard-container">
	<div class="customers-filter">
    	<table class="bookings-filter">
        	<tr>
            <form name="contract_search" action="" method="POST" id="searchClient">
            	<td style="width:20px;">من  </td>
                <td>
                  <input type="text" class="small-input" name="from" id="from" placeholder="" value="<?php if(isset($binds['from']) ) {echo $binds['from'];} ?>" />
                </td>
                <td style="width:40px;">الوحدة</td>
                <td>
                  <select name="apart_id">
                    
                    <?php 
                    if(isset($_SESSION['user_id'])){
                      if(isset($binds['id']) && $binds['id'] == -1)
                        echo "<option selected = 'selected' value='-1'>الجميع</option>";
                      else
                        echo "<option value='-1'>الجميع</option>";
                        
                      $aprtes = query("SELECT id,apart_name FROM apartments WHERE owner_id = ?",$_SESSION['user_id']);
                      foreach($aprtes as $aprt)
                      {
                        if(isset($binds['id']) && $binds['id'] == $aprt['id'] )
                          echo "<option selected='selected' value='{$aprt['id']}'>{$aprt['apart_name']}</option>";
                        else
                          echo "<option  value='{$aprt['id']}'>{$aprt['apart_name']}</option>";
                        
                      }
                    }
                    ?>
                  </select>
                </td>
                <td style="width:95px;">رقم العقد / الحجز</td>
                <td>
                  <input type="text" class="small-input" name="contract_number" value="<?php if(isset($binds['contract_number']) ) {echo $binds['contract_number'];} ?>" />
                </td>
                <td style="dwidth:20px;">
                <select name="contract_status" id="contract_status">
                  <?php 
                    if(isset($binds['contract_status']) ) 
                    { 
                      if($binds['contract_status'] == 1)
                        echo '<option selected = "selected" value="1">العقود السارية</option>';
                      else
                        echo '<option value="1">العقود السارية</option>';
                      
                      if($binds['contract_status'] == 3)
                        echo '<option selected = "selected" value="3">العقود المغلقة</option>';
                      else
                        echo '<option value="3">العقود المغلقة</option>';
                    }
                    else
                    {
                  ?>
                  
                  <option selected = "selected" value="1">العقود السارية</option>
                  <option value="3">العقود المغلقة</option>
                  <?php } ?>
                </select>
                </td>
           
            </tr>
            </table>
            <table style="margin-top:0;" class="bookings-filter">
            <tr>
            	<td>الى</td>
                <td><input type="text" class="small-input" name="to" id="to" placeholder="" value="<?php if(isset($binds['to']) ) {echo $binds['to'];} ?>" /></td>
                <td>العميل</td>
                <td><input type="text" class="small-input" name="client_name" value="<?php if(isset($binds['client_name']) ) {echo $binds['client_name'];} ?>" /></td>
                <td>جوال</td>
                <td><input type="text" class="small-input" name="mobile" value="<?php if(isset($binds['mobile']) ) {echo $binds['mobile'];} ?>"/></td>
                <td style="width:50px;">نوع الايجار</td>
                <td>
                  <select name="rent_type">
                    <?php 
                    if(isset($binds['status']) && $binds['status'] == -1)
                      echo "<option selected = 'selected' value='-1'>الجميع</option>";
                    else
                      echo "<option value='-1'>الجميع</option>";
                    ?>
                    
                    <option <?php if(isset($binds['rent_type']) && $binds['rent_type'] == 0 ) {echo "selected='selected'";} ?> value="0">يومى</option>
                    <option <?php if(isset($binds['rent_type']) && $binds['rent_type'] == 1 ) {echo "selected='selected'";} ?> value="1">شهرى</option>
                  </select>
                </td>
                <td><a href="#"><div class="search-botton">تصدير</div></a></td>
                <td><a href="#"><div class="search-botton">بحث</div></a></td>
                <input type="hidden" id="page" name="page" value="" />
             </form>
            </tr>
        </table>
    </div>
    
    <br />
    
    <div class="dashboard-row">
    
    	<table class="customers-table">
        	<tr class="customers-table-head">
            	<td colspan="4" width=""></td>
                <td colspan="3" width="">الحجز</td>
                <td colspan="2" width="">المطلوب</td>
                <td colspan="3" width="">المالية</td>
            </tr>
            <tr class="customers-table-head">
            	<td width="">رقم العقد / الحجز</td>
                <td width="">الوحدة</td>
                <td width="">العميل</td>
                <td width="">دخول في</td>
            	<td width="">خروج في</td>
                <td width="">المدة</td>
                <td width="">يومي</td>
                <td width="">الاجمالي </td>
                <td width="">المدفوع</td>
                <td width="">دائن</td>
                <td width="">مدين</td>
                <td width="">العمليات</td>
            </tr>
            <?php if(!empty($contracts)) { ?>
            <?php foreach($contracts as $contract) {?>
                <tr class="">
                    <td width=""><?php echo $contract['contract_id']; ?></td>
                    <td class="blue" width=""><?php echo $contract['apart_name']; ?></td>
                    <td class="blue" width=""><?php echo $contract['name']; ?></td>
                    <td width=""><?php echo $contract['mildai_from']; ?></td>
                    <td width=""><?php echo $contract['miladi_out']; ?></td>
                    <td width=""><?php echo $contract['period']; ?></td>
                    <td class="" width="">
                      <?php if($contract['rent_type'] == 0)
                      {
                        $rent = $contract['chosen_daily'];
                        echo $rent;
                      }
                      else if($contract['rent_type'] == 1)
                      {
                        $rent = $contract['chosen_weekly']/7;
                        echo $rent;
                      }
                      else
                      {
                        $rent = $contract['chosen_monthly']/30;
                        echo $rent;
                      }
                      ?>
                    </td>
                    <td width=""> <?php 
                      $total = $contract['total'];
                      echo $contract['total'];
                    ?> 
                    </td>
                    <td width=""><?php 
                      $paid =  $contract['paid'];
                      echo $paid;
                    ?></td>
                    <td class="green bold" width="">
                    <?php 
                      if($paid - $contract['paid_back'] -  $total - $contract['services_revenue']> 0)
                        echo $paid - $contract['paid_back'] -  $total - $contract['services_revenue']; 
                      else
                        echo '-';
                    ?>
                    </td>
                    <td class="red bold" width="">
                    <?php 
                      if($paid - $contract['paid_back'] -  $total - $contract['services_revenue'] < 0)
                        echo $paid - $contract['paid_back'] -  $total - $contract['services_revenue']; 
                      else
                        echo '-';
                    ?>
                    </td>
                   
                    <td class="white-bg">
                     <?php if($binds['contract_status'] != 3) {?>
                      <a href="#view_client<?php echo $contract['contract_id']; ?>" class="view_client_pop"><img src="images/view.png" class="" align="" alt="View"/></a>
                      
                      <!-- $$$$$$$$$$$$$$$ View Pop-up $$$$$$$$$$$$$$$$ -->
                      <div id="view_client<?php echo $contract['contract_id']; ?>" class="zoom-anim-dialog mfp-hide view-booking-popup-edit">
                        <?php require_once("Bookings/view.php"); ?>
                      </div>

                      <a href="#edit_client<?php echo $contract['contract_id']; ?>" contract_id = <?php echo $contract['contract_id']; ?> class="create-booking-pop-up"><img src="images/edit.png" class="margin-right5" align="" alt="Edit"/></a>
                      
                      <!-- $$$$$$$$$$$$$$$ Edit Pop-up $$$$$$$$$$$$$$$$ -->
                      <div id="edit_client<?php echo $contract['contract_id']; ?>" class="zoom-anim-dialog mfp-hide add-booking-popup">
                        <?php require("Bookings/edit.php"); ?>
                      </div>
                      
                        <!--<a href="#" class=""><img src="images/x.png" class="margin-right5" align="" alt="Delete"/></a>-->
                        <?php }else{ ?>
                        <!-- $$$$$$$$$$$$$$$ View Pop-up $$$$$$$$$$$$$$$$ -->
                        <a href="#print_fatora<?php echo $contract['contract_id']; ?>" class="view_client_pop blue">فاتورة<!--<img src="images/view.png" class="" align="" alt="View"/> --></a>
                        <div id="print_fatora<?php echo $contract['contract_id']; ?>" class="zoom-anim-dialog mfp-hide view-booking-popup">
                          <?php require_once("Bookings/fatora.php"); ?>
                        </div><a href="#view_client<?php echo $contract['contract_id']; ?>" class="view_client_pop"><img src="images/view.png" class="" align="" alt="View"/></a>
                        <div id="view_client<?php echo $contract['contract_id']; ?>" class="zoom-anim-dialog mfp-hide view-booking-popup">
                          <?php require_once("Bookings/view.php"); ?>
                        </div>
                        <?php } ?>
                    </td>
                </tr>
              <?php  }
                }else{
                  echo "<td colspan='13' class='large red' align=''>لا توجد نتائج</td>";
                } ?>
            
        </table><br />
        <hr />
        <?php
          $start = $page * 20 -19;
          $end  = $start + 19;
          if($end > $totalPages)
            $end = $totalPages;
        ?>
        <?php if(!empty($contracts)) { ?>
        <div class="clear"></div>
        <div class="relative">
          <div id="contracts-selection" class="center"></div>
          <span class="middle xsmall showing-results absolute">عرض النتائج <?php echo $start; ?> - <?php echo $end; ?> من إجمالى <?php echo $totalPages;?></span>
        </div> 

        <div class="hidden totalPages"><?php if(isset($totalPages)) {echo $totalPages;} ?></div>
        <div class="hidden currentPage"><?php if(isset($page)) {echo $page;} ?></div>
        <?php } ?>
    </div>
    <hr />
    <div class="statistics">
      المدفوع: <?php 
      if( $paid_statistics > 0)
        echo $paid_statistics;
      else
        echo 0;
      ?>
      &nbsp;&nbsp;
      <span style="color:green">دائن : <?php echo $creditor; ?></span>
       &nbsp;&nbsp;
      <span style="color:red">مدين: <?php echo $debitor; ?></span>
    </div>
<div class="clear"></div>
</div>
            
